package com.imist.service.impl;

import com.imist.bean.Article;
import com.imist.bean.ArticleType;
import com.imist.bean.User;
import com.imist.repository.ArticleTypeMapper;
import com.imist.repository.UserMapper;
import com.imist.service.ShopService;
import com.imist.utils.Pager;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("shopService")
public class ShopServiceImpl implements ShopService {

    //得到数据访问层对象
    @Resource
    private ArticleTypeMapper articleTypeMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<ArticleType> getArticleTypes() {
        return articleTypeMapper.getArticleTypes();
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        Map<String,Object> result = new HashMap<>();
        // 1.判断参数是否为null
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            result.put("code",1);
            result.put("msg","参数为空了");
        }else {
            //根据登录名去查询用户对象
            User user  = userMapper.login(username);
            if (user != null){
                //用户名存在，判断密码
                if (user.getPassword().equals(password)){
                    //登陆成功了，将登陆成功的用户存入到session
                    result.put("code",0);
                    result.put("msg",user);
                }else {
                    result.put("code",2);
                    result.put("msg","密码错误");
                }
            }else {
                //登录名不存在
                result.put("code",3);
                result.put("msg","用户不存在");
            }
        }
        return null;
    }

    @Override
    public List<ArticleType> loadFirstArticleTypes() {
        return null;
    }

    @Override
    public List<Article> searchArticles(String typeCode, String secondType, String title, Pager pager) {
        return null;
    }

    @Override
    public List<ArticleType> loadSecondTypes(String typeCode) {
        return null;
    }

    @Override
    public void deleteById(String id) {

    }

    @Override
    public Article getArticleById(String id) {
        return null;
    }

    @Override
    public void updateArticle(Article article) {

    }

    @Override
    public void saveArticle(Article article) {

    }
}
